package grape.composite.rest.expressway.mvc;

import com.baomidou.mybatisplus.core.metadata.IPage;
import grape.common.exception.runtime.RBaseException;
import grape.composite.rest.expressway.form.ExpresswayEntryListForm;
import grape.composite.service.expressway.query.ExpresswayEntryListQuery;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.validation.Valid;
import grape.composite.rest.expressway.form.ExpresswayEntryCreateForm;
import grape.composite.rest.expressway.form.ExpresswayEntryUpdateForm;
import grape.composite.rest.expressway.form.ExpresswayEntryListPageForm;
import grape.composite.rest.expressway.vo.ExpresswayEntryVo;
import grape.composite.rest.expressway.mapper.ExpresswayEntryWebMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import grape.common.rest.mvc.BaseController;
import grape.composite.service.expressway.po.ExpresswayEntry;
import grape.composite.service.expressway.api.IExpresswayEntryService;
import java.util.List;
/**
 * <p>
 * 高速公路 前端控制器
 * </p>
 *
 * @author yangwei
 * @since 2020-09-10
 */
@RestController
@RequestMapping("/composite/expresswayentry")
@Api(tags = "高速公路相关接口")
public class ExpresswayEntryController extends BaseController<ExpresswayEntryVo, ExpresswayEntry> {

    @Autowired
    private ExpresswayEntryWebMapper currentWebMapper;
    @Autowired
    private IExpresswayEntryService currentService;



     @ApiOperation("添加高速公路")
     @PreAuthorize("hasAuthority('expresswayentry:single:create')")
     @PostMapping
     @ResponseStatus(HttpStatus.CREATED)
     public ExpresswayEntryVo create(@RequestBody @Valid ExpresswayEntryCreateForm cf) {
         // code 唯一检查
         if (currentService.existCode(cf.getCode())) {
             throw new RBaseException("编码已存在");
         }
         ExpresswayEntry po = currentWebMapper.formToPo(cf);
         return super.create(po);
     }

     @ApiOperation("根据ID查询高速公路")
     @PreAuthorize("hasAuthority('expresswayentry:single:queryById')")
     @GetMapping("/{id}")
     @ResponseStatus(HttpStatus.OK)
     public ExpresswayEntryVo queryById(@PathVariable String id) {
         return super.queryById(id);
     }

     @ApiOperation("删除高速公路")
     @PreAuthorize("hasAuthority('expresswayentry:single:deleteById')")
     @DeleteMapping("/{id}")
     @ResponseStatus(HttpStatus.NO_CONTENT)
     public boolean deleteById(@PathVariable String id) {
         return super.deleteById(id);
     }

     @ApiOperation("更新高速公路")
     @PreAuthorize("hasAuthority('expresswayentry:single:updateById')")
     @PutMapping("/{id}")
     @ResponseStatus(HttpStatus.CREATED)
     public ExpresswayEntryVo update(@PathVariable String id,@RequestBody @Valid ExpresswayEntryUpdateForm uf) {
         ExpresswayEntry po = currentWebMapper.formToPo(uf);
         po.setId(id);
         return super.update(po);
     }

    @ApiOperation("不分页查询高速公路")
    @PreAuthorize("hasAuthority('expresswayentry:single:list')")
    @GetMapping("/list")
    @ResponseStatus(HttpStatus.OK)
    public List<ExpresswayEntryVo> list(ExpresswayEntryListForm listForm) {
        ExpresswayEntry po = currentWebMapper.formToPo(listForm);
        ExpresswayEntryListQuery expresswayEntryListQuery = new ExpresswayEntryListQuery();
        expresswayEntryListQuery.setPoQuery(po);
        return super.list(expresswayEntryListQuery);
    }

    @ApiOperation("分页查询高速公路")
    @PreAuthorize("hasAuthority('expresswayentry:single:listPage')")
    @GetMapping("/listPage")
    @ResponseStatus(HttpStatus.OK)
    public IPage<ExpresswayEntryVo> listPage(ExpresswayEntryListPageForm listPageForm) {
         ExpresswayEntry po = currentWebMapper.formToPo(listPageForm);
        ExpresswayEntryListQuery expresswayEntryListQuery = new ExpresswayEntryListQuery();
        expresswayEntryListQuery.setPoQuery(po);
         return super.listPage(expresswayEntryListQuery,listPageForm);
     }

}
